#include "Flag.h"


// Material Properties
GLfloat flag1_diffuse[] = {0.30, 0.30, 0.30, 1.0};
GLfloat flag2_diffuse[] = {0.60, 0.60, 0.60, 1.0};

// Shininess
GLfloat flag_shininess[] = { 21.3 };

Flag::Flag(void)
{
	poleHeight = (GLfloat)1/2;
	poleRadius = (GLfloat) 0.015;
	flagWidth = (GLfloat)1/70;
	flagLength = (GLfloat)1/2.7;
	flagHeight = (GLfloat)1/4;
	baseHeight = (GLfloat)1/75;
	baseRadius = (GLfloat)1/25;

	loadImage();
}

//function to load the images to use as textures
void Flag::loadImage() {
	Image* image = loadBMP("textures\\flag.bmp");
	flagTexture = loadTexture(image);
	delete image;
}

void Flag::drawCube(void)
{
	
	glTranslatef(-0.5, -0.5, -0.5);
	glColor3f(0.9, 0.9, 0.9);

	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {(GLfloat)2/3, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {(GLfloat)2/3, 1.0f};

	// top
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 1.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(A);
		glTexCoord2fv(bottomRight);
		glVertex3fv(B);
		glTexCoord2fv(topRight);
		glVertex3fv(C);
		glTexCoord2fv(topLeft);
		glVertex3fv(D);
	glEnd();
	
	// bottom
	glBegin(GL_QUADS);
		glNormal3f( 0.0, -1.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(H);
		glTexCoord2fv(bottomRight);
		glVertex3fv(G);
		glTexCoord2fv(topRight);
		glVertex3fv(F);
		glTexCoord2fv(topLeft);
		glVertex3fv(E);
	glEnd();
	
	// Front
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 0.0, 1.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(E);
		glTexCoord2fv(bottomRight);
		glVertex3fv(F);
		glTexCoord2fv(topRight);
		glVertex3fv(B);
		glTexCoord2fv(topLeft);
		glVertex3fv(A);
	glEnd();

	// back
	glBegin(GL_QUADS);
		glNormal3f( 0.0, 0.0, -1.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(G);
		glTexCoord2fv(bottomRight);
		glVertex3fv(H);
		glTexCoord2fv(topRight);
		glVertex3fv(D);
		glTexCoord2fv(topLeft);
		glVertex3fv(C);
	glEnd();

	// left
	glBegin(GL_QUADS);
		glNormal3f( -1.0, 0.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(H);
		glTexCoord2fv(bottomRight);
		glVertex3fv(E);
		glTexCoord2fv(topRight);
		glVertex3fv(A);
		glTexCoord2fv(topLeft);
		glVertex3fv(D);
	glEnd();

	// right
	glBegin(GL_QUADS);
		glNormal3f( 1.0, 0.0, 0.0);
		glTexCoord2fv(bottomLeft);
		glVertex3fv(F);
		glTexCoord2fv(bottomRight);
		glVertex3fv(G);
		glTexCoord2fv(topRight);
		glVertex3fv(C);
		glTexCoord2fv(topLeft);
		glVertex3fv(B);
	glEnd();

}
void Flag::display(int up, int left)
{
	glPushMatrix();
		glRotatef((GLfloat) left, 0.0, 1.0, 0.0);
		glRotatef((GLfloat) up, 1.0, 0.0, 0.0);

		// BASE
		glPushMatrix();
			glMaterialfv(GL_FRONT, GL_DIFFUSE, flag1_diffuse);
			glColor3f(0.3,0.3,0.3);
			glPushMatrix();
				glRotatef(-90, 1.0, 0.0, 0.0);
				GLUquadricObj* quad1 = gluNewQuadric();
				gluCylinder (quad1, baseRadius, baseRadius, baseHeight, 30, 3);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0.0, baseHeight, 0.0);
				glRotatef(-90, 1.0, 0.0, 0.0);
				GLUquadricObj* quad2 = gluNewQuadric();
				gluDisk(quad2, 0.0, baseRadius, 30, 3);
			glPopMatrix();
			glPushMatrix();
				glRotatef(-90, 1.0, 0.0, 0.0);
				GLUquadricObj* quad3 = gluNewQuadric();
				gluDisk(quad3, 0.0, baseRadius, 30, 3);
			glPopMatrix();
		glPopMatrix();

		// draw pole	
		glColor3f(0.6,0.6,0.6);
		glPushMatrix();
			glMaterialfv(GL_FRONT, GL_SHININESS, flag_shininess);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, flag2_diffuse);
			glTranslatef(0,baseHeight,0);
			glRotatef(-90, 1,0,0);
			GLUquadricObj* quad = gluNewQuadric();
			gluCylinder(quad, poleRadius, poleRadius, poleHeight, 10, 30);
		glPopMatrix();
		
		// flag	
		glBindTexture(GL_TEXTURE_2D, flagTexture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glColor3f(0.3,0.3,0.3);
		glPushMatrix();	
			glMaterialfv(GL_FRONT, GL_DIFFUSE, flag1_diffuse);
			glTranslatef(flagLength/2, poleHeight/1.4 + baseHeight, 0);
			glScalef(flagLength, flagHeight, flagWidth);
			drawCube();
		glPopMatrix();

		
	glPopMatrix();
}

Flag::~Flag(void)
{
}
